package com.jisheng.xb.web;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.I0Itec.zkclient.DataUpdater;
import org.apache.ibatis.annotations.Param;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.alibaba.fastjson.JSONObject;
import com.jisheng.xb.common.DateUtil;
import com.jisheng.xb.dao.ProrecordMapper;
import com.jisheng.xb.entity.Prorecord;


@Controller
@RequestMapping("prorecord")
public class ProrecordController {
	
	final static Logger log = LoggerFactory.getLogger(ProrecordController.class);

	@Autowired
	ProrecordMapper prorecordMapper ;
	
	@RequestMapping(value = {"list"}, method = { RequestMethod.POST,RequestMethod.GET})
	public ModelAndView list(Model model,
			HttpServletRequest request,
			HttpServletResponse response){
		List<Prorecord> list = new ArrayList<Prorecord>();
		//TODO 登录模块好了之后从session中获取
		list = prorecordMapper.selectByAuthorAndWeek("admin",DateUtil.getCurrentTimeWeekNum());
		List<Prorecord> listMonday = new ArrayList<Prorecord>();
		List<Prorecord> listTuesday = new ArrayList<Prorecord>();
		List<Prorecord> listWednesday = new ArrayList<Prorecord>();
		List<Prorecord> listThursday = new ArrayList<Prorecord>();
		List<Prorecord> listFriday = new ArrayList<Prorecord>();
		List<Prorecord> listSaturday = new ArrayList<Prorecord>();
		List<Prorecord> listSunday = new ArrayList<Prorecord>();
		List<Prorecord>[] arrList = new List[7];
		arrList[0] = listSunday;
		arrList[1] = listMonday;
		arrList[2] = listTuesday;
		arrList[3] = listWednesday;
		arrList[4] = listThursday;
		arrList[5] = listFriday;
		arrList[6] = listSaturday;
		for(Prorecord p : list){
			arrList[p.getWeekday()].add(p);
		}
		
		
		ModelAndView mv = new ModelAndView("prorecord").addObject("arrList",arrList);
		return mv;
	}
	
	@RequestMapping(value = {"getListByWeekDay"}, method = { RequestMethod.POST,RequestMethod.GET})
	@ResponseBody
	public String getListByWeekDay(Model model,@Param("weekDay")Integer weekDay,
			HttpServletRequest request,
			HttpServletResponse response){
		List<Prorecord> list = new ArrayList<Prorecord>();
		//TODO 登录模块好了之后从session中获取
		list = prorecordMapper.selectByAuthorAndWeekInfo("admin", weekDay,DateUtil.getCurrentTimeWeekNum());
		request.setAttribute("list", list);
		return JSONObject.toJSONString("刷新成功！");
	}
	
	@RequestMapping(value={"save"},method={RequestMethod.POST,RequestMethod.GET})
	@ResponseBody
	public String  save(Prorecord prorecord,
			HttpServletRequest request,
			HttpServletResponse response){
		//TODO 暂时将记录人设置成admin 后期从session获取
		prorecord.setAuthor("admin");
		prorecord.setInsertTime(new Date());
		prorecord.setWeek(DateUtil.getCurrentTimeWeekNum());
		String msg = "保存成功";
		prorecordMapper.insert(prorecord);
		return JSONObject.toJSONString(msg);
		                              
	} 
	
	@RequestMapping(value={"update"},method={RequestMethod.POST,RequestMethod.GET})
	@ResponseBody
	public String  update(Prorecord prorecord,
			HttpServletRequest request,
			HttpServletResponse response){
		String msg = "更新成功";
		if(prorecord.getId()==null){
			msg = "更新失败";
			return JSONObject.toJSONString(msg);
		}
		prorecordMapper.updateByPrimaryKeySelective(prorecord);
		return JSONObject.toJSONString(msg);
		                              
	} 
	
	@RequestMapping(value={"delete"},method={RequestMethod.POST,RequestMethod.GET})
	@ResponseBody
	public String  delete(@RequestParam Long id,
			HttpServletRequest request,
			HttpServletResponse response){
		String msg = "删除成功";
		prorecordMapper.deleteByPrimaryKey(id);
		return JSONObject.toJSONString(msg);
		                              
	}
}
